#include <Driver.h>

#include <sybdb.h>

#include <vector>

namespace{
    int err_handler(DBPROCESS* dbproc, int severity, int dberr, int oserr, char* dberrstr, char* oserrstr){
        return INT_CANCEL;
    }
    bool is_alive(const CExpert::DBChecker::conn_params& cp){
        LOGINREC *login;
        DBPROCESS *dbproc;

        if (dbinit() != SUCCEED)
            return false;
        dberrhandle(&err_handler);

        login = dblogin();
        if (!login){
            dbexit();
            return false;
        }
        DBSETLUSER(login, cp.username.c_str());
        DBSETLPWD(login, cp.passwd.c_str());
        dbsetlogintime(1);

        std::vector<char> db_str(cp.db.begin(), cp.db.end());
        dbproc = dbopen(login, &db_str[0]);

        dbexit();
        return dbproc;
    }
}
void mssql_init(){
    static int mssql_init_v = CExpert::register_driver("mssql", &is_alive);
    (void)mssql_init_v;
}
